﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Model;
using Utils;
using Microsoft.Phone.Shell;

namespace HowHigh
{
    public partial class TestPage : PhoneApplicationPage
    {
        Project currentProject;
        public TestPage()
        {
            InitializeComponent();
            TitlePanel.DataContext = App.Current.AppInfo;
            button1.DataContext = App.Current.AppInfo;
            button2.DataContext = App.Current.AppInfo;
            textBlock4.DataContext = App.Current.AppInfo;
            Loaded += new RoutedEventHandler(TestPage_Loaded);
            SystemTray.SetIsVisible(this, true);
            SystemTray.SetBackgroundColor(this, Colors.Black);
            SystemTray.SetOpacity(this, 0.5);
            if (App.Current.CurrentProgressIndicator == null)
            {
                App.Current.CurrentProgressIndicator = new ProgressIndicator()
                {
                    IsIndeterminate = true
                };
            }

            SystemTray.SetProgressIndicator(this, App.Current.CurrentProgressIndicator);
        }

        void TestPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.Current.CurrentProject == null)
            {
                currentProject = Project.New();
                currentProject.Add();
                App.Current.CurrentProject = currentProject;
            }
            currentProject = App.Current.CurrentProject;
            ContentPanel.DataContext = currentProject;
            currentProject.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(currentProject_PropertyChanged);
        }

        void currentProject_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Target")
            {
                map1.AddLocation(currentProject.Target);
            }
            else if (e.PropertyName == "FirstLocation")
            {
                map1.AddLocation(currentProject.FirstLocation);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GetDegree.xaml", UriKind.Relative));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Code.GpsHelper.SetLocation(Dispatcher,loc =>
            {
                currentProject.Target = loc;
                currentProject.Update();
            });
        }
    }
}